<?php

require_once("./lib/pluginbase.php");
require_once("./cfg/item_data.php");

class SpawnPlugin extends PluginBase
{
	protected $spawncd;
	function processChatCommand($name, $cmd, $args)
	{
		global $item_data;
		global $item_names;
		$admin = $this->isAdmin($name);
		switch ($cmd)
		{
			case "!spawn":
				$now = time();
				if (!$admin && isset($this->spawncd[$name]) && ($now - $this->spawncd[$name] < 10))
				{
					echo "say Sorry, Tom Brokaw, you gotta wait 10 seconds between spawning...\n";
					break;
				}
				if(count($args) < 1)
				{
					echo "say Yeah, but spawn what (by id)?\n";
					break;
				}
				$item = $args[0];
				if(!is_numeric($args[0]))
				{
					if (isset($item_names[strtolower($args[0])]))
					{
						$item = $item_names[strtolower($args[0])];
					}
					else
					{
						$found = false;
						foreach ($item_data as $id => $item_array)
						{
							if (str_replace(" ","",strtolower($item_array["name"])) == strtolower($args[0]))
							{
								$found = true;
								$item = $id;
								break;
							}
						}
						if (!$found)
						{
							echo "say Item not regonized, also you're fat and nobody likes you.\n";
							break;
						}
					}
				}
				if(isset($item_data[$item]) && isset($item_data[$item]["blacklisted"]) && $item_data[$item]["blacklisted"])
				{
					echo "say Kicking " . $name . " for trying to spawn a blacklisted item.\n";
					echo "kick" . $name . "\n";
					break;
				}
				if(!$admin && (!isset($item_data[$item]) || !isset($item_data[$item]["spawnable"]) || !($item_data[$item]["spawnable"])))
				{
					if(isset($item_data[$item]) && isset($item_data[$item]["spawnoverride"]))
					{
						if(in_array($name,explode(',',$item_data[$item]["spawnoverride"])))
						{
							echo "say WHITELIST OVERRIDE ACCEPTED!\n";
						}
						else
						{
							echo "say Item " . $item . " isn't whitelisted, die in a fire.\n";
							break;
						}
					}
					else
					{
						echo "say Item " . $item . " isn't whitelisted, die in a fire.\n";
						break;
					}
				}
				$qty = 64;
				if(isset($item_data[$item]["defaultspawn"]))
				{
					$qty = $item_data[$item]["defaultspawn"];
				}
				if(count($args) > 1 && is_numeric($args[1]))
				{
					$max = 128;
					if(isset($item_data[$item]) && isset($item_data[$item]["maxspawn"]))
					{
						$max = $item_data[$item]["maxspawn"];
					}
					if(!$admin && $args[1] > $max)
					{
						echo "say Sorry, you can only spawn a maximum of " . $max . "...\n";
						$qty = $max;
					}
					else
					{
						$qty = $args[1];
					}
				}
				if (isset($item_data[$item]) && isset($item_data[$item]["name"]))
				{
					echo "say Alright, ". $item_data[$item]["name"] . " coming right up!\n";
				}
				else
				{
					echo "say Alright, coming right up!\n";
				}
				$this->give($name, $item , $qty);
				$this->spawncd[$name] = time();
				break;
		}
	}
}

registerPlugin("spawn", new SpawnPlugin);


